﻿using UnityEngine;
using System.Collections;

public class UIScaleAnim : MonoBehaviour
{

    FSM scalefsm = new FSM();
    StatePath scaleidle = new StatePath("scaleidle");
    StatePath scaledown = new StatePath("scaledown");
    StatePath scaleout = new StatePath("scaleout");
    StatePath scalein = new StatePath("scalein");
    StatePath scalesleep = new StatePath("scalesleep");

    Vector3 targetScale = Vector3.one;
    Vector3 currentSpeed = Vector3.zero;
    float moveSpeed = 0.05f;
    Transform cachedTransform;
    // Use this for initialization
    void Awake()
    {

        cachedTransform = transform;

        scalefsm.AddState(scaleidle, OnScaleIdleEnter,OnScaleIdleUpdate);
        scalefsm.AddState(scaledown, OnScaleDownEnter, OnScaleDownUpdate);
        scalefsm.AddState(scaleout, OnScaleOutEnter, OnScaleOutUpdate);
        scalefsm.AddState(scalein, OnScaleInEnter, OnScaleInUpdate,OnScaleInExit);
        scalefsm.AddState(scalesleep);
        scalefsm.SetState(scalesleep);
    }
    //void OnGUI()
    //{
    //    if (GUILayout.Button("压"))
    //        OnPress();
    //    if (GUILayout.Button("变大"))
    //        OnScaleOut();
    //    if (GUILayout.Button("变大回弹"))
    //        OnScaleIn();
    //    if (GUILayout.Button("恢复"))
    //        OnResetScale();
    //}
    // Update is called once per frame
    void Update()
    {
        scalefsm.UpdateState(Time.deltaTime);
    }
    //按压变小
    public void OnPress()
    {
        scalefsm.SetState(scaledown);
    }
    /// <summary>
    /// 放开变大
    /// </summary>
    public void OnScaleOut()
    {
        scalefsm.SetState(scaleout);
    }
    /// <summary>
    /// 放开变大后反弹缩小
    /// </summary>
    void OnScaleIn()
    {
        scalefsm.SetState(scalein);
    }
    /// <summary>
    /// 恢复到原大小
    /// </summary>
    public void OnResetScale()
    {
        scalefsm.SetState(scaleidle);
    }
    #region scale
    void OnScaleIdleEnter()
    {
        targetScale = new Vector3(1f, 1f, 1f); 
    }

    void OnScaleIdleUpdate(float dt) {

        UpdateScale();
    }

    void OnScaleDownEnter()
    {
        targetScale = new Vector3(0.8f, 0.8f, 1f);
        
    }

    void OnScaleDownUpdate(float dt)
    {
        UpdateScale();
    }

    void OnScaleOutEnter()
    {
        targetScale = new Vector3(1.5f, 1.5f, 1f);
    }

    void OnScaleOutUpdate(float dt)
    {

        UpdateScale();
        if(scalefsm.stateTime > 0.1f)
            scalefsm.SetState(scalein);
    }

    void OnScaleInEnter()
    {
        moveSpeed = 0.02f;
        targetScale = new Vector3(1.38f, 1.38f, 1f);

    }

    void OnScaleInUpdate(float dt)
    {

        UpdateScale();

    }
    void OnScaleInExit() {

        moveSpeed = 0.05f;
    }


    void UpdateScale()
    {
        cachedTransform.localScale = Vector3.SmoothDamp(cachedTransform.localScale, targetScale, ref currentSpeed, moveSpeed);
        if (Mathf.Abs(currentSpeed.x) < 0.1f) {
            if (scalefsm.IsState(scaleout))
            {scalefsm.SetState(scalein);}
            else { scalefsm.SetState(scalesleep); }
        }
    }
    #endregion

}
